package service;

import dao.AdminDao;
import dao.AdminDaoImpl;
import dao.RoomDao;
import dao.RoomDaoImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.Admin;
import pojo.Bed;
import pojo.CheckInRecord;
import pojo.Room;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.jar.JarEntry;

public class RoomServiceImpl implements RoomService{
    private RoomDao dao = new RoomDaoImpl();
    private Connection con = null;
    private Log log = LogFactory.getLog(getClass());
    private BedService bedService = new BedServiceImpl();

    @Override
    public ArrayList<Room> findAll() {
        return dao.findAll();
    }

    @Override
    public int findRoomTotal() {
        return dao.findAll().size();
    }

    @Override
    public ArrayList<Room> findBySex(String sex) {
        ArrayList<Room> list = dao.findAll();
        ArrayList<Room> rooms = new ArrayList<Room>();
        for (Room room : list) {
            if(room.getSex().equals(sex)){
                rooms.add(room);
            }
        }
        return rooms;
    }

    @Override
    public Room findById(Integer dormId) {
        return dao.findById(dormId);
    }

    @Override
    public int insert(Room room) {
        return dao.insert(room);
    }

    @Override
    public int update(Room room) {
        return dao.update(room);
    }

    @Override
    public int updateSize(Room room) {
        con = JDBCUtils.getConnection();
        try {
            con.setAutoCommit(false);
            //获取原本大小
            int oldSize = dao.findById(room.getRoomId()).getSize();
            int margin = room.getSize()-oldSize;

            int beginBedId = 1;
            String sql = "SELECT MAX(bedId) FROM bed WHERE dormId = ?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1,room.getRoomId());
            int result = ps.executeUpdate();

            ResultSet rs = ps.getResultSet();
            while (rs.next()){
                beginBedId = rs.getInt(1);
            }
            for(int i=0;i<margin;i++){
                bedService.insert(new Bed(room.getRoomId(),beginBedId,false));
            }
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            try {
                con.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public int delete(Room room) {
        try {
            con = JDBCUtils.getConnection();
            con.setAutoCommit(false);

            int row = dao.delete(con,room);
            con.commit();
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            try {
                con.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            JDBCUtils.close(con,null);
        }
        return 0;
    }

}
